package com.example.spring_seatreservation.mapper;


import com.example.spring_seatreservation.Bean.MyUser;
import com.example.spring_seatreservation.entity.Seat;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface PublicMapper {


    @Insert("insert into user(number,username,password,type,sex,birth,phone,email,photo,faculty,profession,create_time,role_id) " +
            "values(${number},#{username},#{password},${type},#{sex},#{birth},#{phone},#{email},#{photo},#{faculty},#{profession},now(),#{roleId})")
    void insertUser(MyUser user);

    @Select("select * from user where number=#{number}")
    MyUser getUserByNumber(String number);

    @Update("update user set password=#{password} where number=#{number}")
    void updatePwd(String password, String number);

    @Select("select * from area")
    List<Map<String, Object>> getArea();

    @Select("<script>" +
            "select * from seat where 1=1 " +
            "<if test=\"area != '' and area != null\">" +
            " and area=${area}" +
            "</if>" +
            "<if test=\"roomId != '' and roomId != null\">" +
            " and r_room_id=${roomId}" +
            "</if>" +
            "</script>")
    List<Map<String, Object>> getAreaSeats(Map<String, Object> map);

    @Select("SELECT t.sid,t.type,t.row,t.column,t.r_room_id," +
            "(select count(1) from reservation r where t.sid = r.sid and r.state > -1 and order_date = #{orderDate}) as state " +
            "from seat t " +
            "where t.r_room_id = #{roomId}")
    List<Seat> getRoomSeats(Map<String, Object> map);

    @Select("SELECT * FROM reservation WHERE state!=-1 and state != 4")
    List<Map<String, Object>> getNeedCheckReservation();

    @Update("update reservation set state=${state} where rid=${rid}")
    void updateReservation(Object state, Object rid);

    @Select("SELECT * FROM reservation WHERE sid=${sid} AND state=0 OR state=3")
    Map<String, Object> getReservationBySid(Object rid);
}
